<template>
<table
    class="water-cp-container"
    border="1">
    <tr class="top-container">
        <td class="left">
            特征离子剖面 基础数据
        </td>
        <td class="right">
            当前报告
        </td>
    </tr>
    <tr class="content-container">
        <td class="left">
            <div class="container">
                <table border="1">
                    <tr>
                        <th rowspan="2">层系</th>
                        <th 
                            v-for="(item,index) in Object.keys(ionIndex)"
                            :key="index"
                            rowspan="2">
                            {{ionIndex[item].name}}
                            <div class="kedu">
                                <span>{{ionIndex[item].minMax[0]}}</span>
                                <span>g/L</span>
                                <span>{{ionIndex[item].minMax[1]}}</span>
                            </div>
                        </th>
                        <th :colspan="baseData.cols.length">地层水地球化学相图</th>
                    </tr>
                    <tr>
                        <th
                            v-for="(item,index) in baseData.cols"
                            :key="index">
                            {{item}}
                        </th>
                    </tr>
                    <tr
                        v-for="(item,index) in baseData.dataList"
                        :key="index">
                        <th>{{index}}</th>
                        <th
                            v-for="(item_,index_) in Object.keys(ionIndex)"
                            :key="index_">
                            <BoxChart
                                class="box-chart"
                                :minMax="ionIndex[item_].minMax"
                                :borderColor="getRowColor(index)"
                                :baseData="toUnit(item[item_],ionIndex[item_].unit)">
                            </BoxChart>
                        </th>
                        <th
                            v-for="(item_,index_) in baseData.cols"
                            :key="index_+8">
                            <NovakChart
                                class="novak-chart"
                                v-if="baseData.rows[index]"
                                :xAxis="false"
                                :padding='[0,0,0,0]'
                                :pointData="dataToPoint(baseData.rows[index][item_])">
                            </NovakChart>
                        </th>
                    </tr>
                </table>
            </div>
        </td>
        <td class="right">
            <div class="chart">
                <table border="1">
                    <tr>
                        <th rowspan="2">层系</th>
                        <th 
                            v-for="(item,index) in Object.keys(ionIndex)"
                            :key="index"
                            rowspan="2">
                            {{ionIndex[item].name}}
                            <div class="kedu">
                                <span>{{ionIndex[item].minMax[0]}}</span>
                                <span>g/L</span>
                                <span>{{ionIndex[item].minMax[1]}}</span>
                            </div>
                        </th>
                        <th :colspan="reportData.cols.length">地层水地球化学相图</th>
                    </tr>
                    <tr>
                        <th
                            v-for="(item,index) in reportData.cols"
                            :key="index">
                            {{item}}
                        </th>
                    </tr>
                    <tr
                        v-for="(item,index) in reportData.dataList"
                        :key="index">
                        <th>{{index}}</th>
                        <th
                            v-for="(item_,index_) in Object.keys(ionIndex)"
                            :key="index_">
                            <BoxChart
                                class="box-chart"
                                :minMax="ionIndex[item_].minMax"
                                :borderColor="getRowColor(index)"
                                :baseData="toUnit(item[item_],ionIndex[item_].unit)">
                            </BoxChart>
                        </th>
                        <th
                            v-for="(item_,index_) in reportData.cols"
                            :key="index_+8">
                            <NovakChart
                                class="novak-chart"
                                v-if="reportData.rows[index]"
                                :xAxis="false"
                                :padding='[0,0,0,0]'
                                :pointData="dataToPoint(reportData.rows[index][item_])">
                            </NovakChart>
                        </th>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
</table>
</template>
<script>
/*
 特征离子剖面展示页面
 */
import BoxChart from "./featuresIon/BoxChart";
import NovakChart from '@/components/novak/NovakChart';
import {randomColor} from "@/common/ColorTools";
export default {
    components:{BoxChart,NovakChart},
    props:{
        reports:{  //当前选择的报告列表
            type:Array,
            default:()=>[],
        },
        areas:{  //当前选择的井号
            type:Array,
            default:()=>[],
        },
        horizons:{  //当前选择的层位
            type:Array,
            default:()=>[],
        },
        fontColor:{
            type:String,
            default:'',
        },
        borderColor:{
            type:String,
            default:'',
        },
        chartBorderColor:{
            type:String,
            default:'',
        },
    },
    data() {
        this.ionIndex = {  //行数据中的离子index，以及它的单位，以及它上下限
            'k_lon':{
                minMax:[0,6],
                name:'钾',
                unit:'g/L',
            },
            'ca_lon':{
                minMax:[0,35],
                name:'钙',
                unit:'g/L',
            },
            'ba_lon':{
                minMax:[0,3500],
                name:'钡',
                unit:'mg/L',
            },
            'sr_lon':{
                minMax:[0,3000],
                name:'锶',
                unit:'mg/L',
            },
            'so4_lon':{
                minMax:[0,6000],
                name:'硫酸根',
                unit:'mg/L',
            },
            'br_lon':{
                minMax:[0,3000],
                name:'溴',
                unit:'mg/L',
            },
            'b_lon':{
                minMax:[0,1200],
                name:'硼',
                unit:'mg/L',
            },
            'mgl':{
                minMax:[0,350],
                name:'矿化度',
                unit:'g/L',
            },
        };
        return {
            baseData:{  //基础数据
                rows:{},
                cols:[],
                dataList:{},
            },
            reportData:{
                rows:{},
                cols:[],
                dataList:{},
            },
            colColors:{},  //行数据配色
        };
    },
    watch:{
        reports:{
            handler(){
                this.$nextTick(()=>{
                    this.getReportData();
                    this.getBaseData();
                });
            },
            immediate:true,
        },
    },
    mounted(){
        // this.getBaseData();
    },
    methods:{
        toUnit(values,unit='g/L'){  //转换单位,数据单位mg/L
            if(unit=='g/L'){
                values = values.map(item=>{
                    return item/1000;
                });
            }
            return values;
        },
        getRowColor(index){  //每行中的颜色，根据行的index
            if(!this.colColors[index]){
                this.colColors[index] = randomColor();
            }
            return this.colColors[index];
        },
        getBaseData(){  //获取基础数据
            if(this.reports.length == 0){
                this.baseData.rows = {};
                this.baseData.cols = [];
                this.baseData.dataList = {};
                return;
            }
            this.$Service.post(  //获取基础的罗瓦克相图
                this.$Api.chemical,
                {
                    isbasic:1,
                    'area':this.areas.join(','),
                    'horizon':this.horizons.join(','),
                },
            ).then(data => {
                let dataList = data.data;
                let rows = {};  //行数据
                let cols =[];  //列数据
                //数据格式化
                dataList.forEach(item => {
                    rows[item.horizon] = rows[item.horizon] || {};  //初始化
                    rows[item.horizon][item.area] = item;
                    (!cols.includes(item.area)) && cols.push(item.area);
                });
                this.baseData.rows = rows;
                this.baseData.cols = cols;
            }).catch(() => {
                return;
            });
            this.$Service.post(
                this.$Api.featuresLon,
                {
                    "isbasic":1,
                    'area':this.areas.join(','),
                    'horizon':this.horizons.join(','),
                },
            ).then(data => {
                this.baseData.dataList = data.data;
            }).catch(() => {
                return;
            });
        },
        getReportData(){  //获取报告数据
            if(this.reports.length == 0){
                this.reportData.rows = {};
                this.reportData.cols = [];
                this.reportData.dataList = {};
                return;
            }
            this.$Service.post(  //获取基础的罗瓦克相图
                this.$Api.chemical,
                {
                    "reportType":1,
                    "reportNo":this.reports.map(item=>item.reportNo).join(","),
                    "contenid":this.reports.map(item=>item.contenid).join(","),
                    'area':this.areas.join(','),
                    'horizon':this.horizons.join(','),
                },
            ).then(data => {
                let dataList = data.data;
                let rows = {};  //行数据
                let cols =[];  //列数据
                //数据格式化
                dataList.forEach(item => {
                    rows[item.horizon] = rows[item.horizon] || {};  //初始化
                    rows[item.horizon][item.area] = item;
                    (!cols.includes(item.area)) && cols.push(item.area);
                });
                this.reportData.rows = rows;
                this.reportData.cols = cols;
            }).catch(() => {
                return;
            });
            this.$Service.post(
                this.$Api.featuresLon,
                {
                    "reportType":1,
                    "reportNo":this.reports.map(item=>item.reportNo).join(","),
                    "contenid":this.reports.map(item=>item.contenid).join(","),
                    'area':this.areas.join(','),
                    'horizon':this.horizons.join(','),
                },
            ).then(data => {
                this.reportData.dataList = data.data;
            }).catch(() => {
                return;
            });
        },
        dataToPoint(novak){  //数据转点数据
            if(!novak) return [];
            return [novak.ca_mg,novak.na_ca,novak.na_mg,novak.na_k_ca_mg,novak.mg_k,novak.na_k];
        },
    },
};
</script>
<style scoped lang="scss">
.water-cp-container{
    width: 100%;
    height: fit-content;
    text-align: left;
    color: var(--font-color);
    box-sizing: border-box;
    border-collapse: collapse;
    border-color: var(--border-color);
    border: 1px solid var(--border-color);
    table-layout:fixed;
    .box-chart{
        width: 70px;
        height: 2rem;
    }
    .novak-chart{
        width: 50px;
        height: 3rem;
    }
    th{
        min-width: 70px;
    }
    >tr{
        >td{
            vertical-align: top;
            &:last-child{
                width: 50%;
            }
        }
    }
    >.top-container{
        >td{
            font-size: 1.5rem;
            font-weight: bold;
            padding: 1rem;
            box-sizing: border-box;
            line-height: 1;
            box-sizing: border-box;
        }
    }
    >.content-container{
        .base-data-title{
            font-size: 1.5rem;
            text-align: left;
        }
        .kedu{
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 0.1rem;
            margin-top: 0.5rem;
            color: #7b7b7b;
        }
        table{
            width: 100%;
            height: fit-content;
            border-collapse: collapse;
            border-color: #0f4b78;
            border: 1px solid #0f4b78;
            .table-bg{
                background-color: #ffee00;
                color: #0f4b78;
            }
            td,th{
                padding: 0.3rem;
                text-align: center;
                font-size: 0.8rem;
                box-sizing: border-box;
                border: 1px solid var(--border-color);
            }
            th{
                font-size: 1rem;
                font-weight: bold;
                width: calc(100% / 13);
            }
        }
        >.left{
            >.container{
                max-height: 30rem;
                overflow: auto;
                padding: 1rem;
                box-sizing: border-box;
                width: 100%;
                text-align: center;
                display: grid;
                grid-gap: 1rem;
                >.base-data-title{
                    font-size: 1.5rem;
                }
            }
        }
        >.right{
            >.chart{
                max-height: 30rem;
                overflow: auto;
                padding: 1rem;
                box-sizing: border-box;
                display: grid;
                grid-gap: 1rem;
            }
        }
    }
    >.bottom-container{
        >.container{
            padding: 1.5rem 1rem;
            font-size: 1.5rem;
            color: #99a9bf;
            box-sizing: border-box;
            span{
                font-size: 1.7rem;
                color: var(--font-color);
            }
            .data{
                font-size: 2rem;
                color: #00c57a;
                font-weight: bold;
            }
        }
    }
}
</style>
